﻿using System.Net;
using System.Net.Mail;

namespace INHollandair.Core.Models
{
    public class Mailer
    {
        private readonly SmtpClient _mailClient;

        public Mailer()
        {
            _mailClient = new SmtpClient
            {
                Host = "smtp.gmail.com",
                Port = 587,
                EnableSsl = true,
                Credentials = new NetworkCredential("inhollandair", "MAYBEIDON'TWANTASAFEPW")
            };
        }

        private const string GenericMailBodyStart = "Geachte ";
        private const string GenericMailBodyStartSpacer = ", <br /><br />";
        private const string GenericMailBodyEnd = "<br /><br />Met vriendelijke groet, <br />Henkiepenkie, hoofd klantenservice INHollandair<br /><a href=\"http://localhost:51000\">www.INHollandair.nl</a>";

        private const string RegistrationSubject = "INHollandair - Registratie";
        private const string RegistrationBody = "Dank u voor uw registratie, klik op de onderstaande link om uw account te activeren.<br />";
        private const string RegistrationUrl = "https://inhollandair.nl/Account/ConfirmRegistration.aspx?RegisterUrl=";

        private const string ResetPasswordSubject = "INhollandair - Wachtwoord reset";
        private const string ResetPasswordBody = "U ontvangt deze mail in reactie op uw aanvraag om uw wachtwoord te resetten, klik op de onderstaande link om uw wachtwoord te resetten.<br />";
        private const string ResetPasswordUrl = "https://inhollandair.nl/Account/ResetPassword.aspx?ResetPasswordUrl=";

        private const string ConfirmOrderSubject = "INhollandair - Order bevestiging";
        private const string ConfirmOrderBody = "U ontvangt deze mail in reactie op uw bestelling, klik op de onderstaande link om uw bestelling te bevestigen.<br />";
        private const string ConfirmOrderUrl = "https://inhollandair.nl/Shop/ConfirmOrder.aspx?OrderUrl=";
        
        public void SendRegistrationMail(User user, RegisterURL url)
        {
            MailMessage message = new MailMessage();
            message.From = new MailAddress("INHollandair@gmail.com");
            message.To.Add(user.Email);

            message.Subject = RegistrationSubject;
            message.Body =  GenericMailBodyStart +
                            user.Name +
                            GenericMailBodyStartSpacer +
                            RegistrationBody +
                            RegistrationUrl +
                            url.RegisterUrl +
                            GenericMailBodyEnd;

            message.IsBodyHtml = true;

            _mailClient.Send(message);
        }

        public void SendForgotPasswordMail(User user, ResetPasswordURL url)
        {
            MailMessage message = new MailMessage();
            message.From = new MailAddress("INHollandair@gmail.com");
            message.To.Add(user.Email);

            message.Subject = ResetPasswordSubject;
            message.Body =  GenericMailBodyStart +
                            user.Name +
                            GenericMailBodyStartSpacer +
                            ResetPasswordBody +
                            ResetPasswordUrl +
                            url.ResetPasswordUrl +
                            GenericMailBodyEnd;

            message.IsBodyHtml = true;

            _mailClient.Send(message);
        }

        public void SendOrderConfirmMail(User user, Product product, string url)
        {
            //Set order table layout
            string confirmOrderTable = "<table>\n<tr><td width=\"10%\"><strong>ID</strong></td><td width=\"70%\"><strong>Name</strong></td><td width=\"20%\"><strong>Price</strong></td></tr>\n<tr><td>" + product.Id.ToString() + "</td><td>" + product.Name + "</td><td>€" + product.Price + "</td></tr>\n</table>";

            //Send email
            MailMessage message = new MailMessage();
            message.From = new MailAddress("INHollandair@gmail.com");
            message.To.Add(user.Email);

            message.Subject = ConfirmOrderSubject;
            message.Body =  GenericMailBodyStart +
                            user.Name +
                            GenericMailBodyStartSpacer +
                            ConfirmOrderBody +
                            ConfirmOrderUrl +
                            url +
                            GenericMailBodyStartSpacer +
                            confirmOrderTable +
                            GenericMailBodyEnd;
            message.IsBodyHtml = true;

            _mailClient.Send(message);
        }
    }
}
